﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using POSSystem.BLL;
using ShopOnline.BLL;
using MyGeneration.dOOdads;
using System.IO;
using System.Web.Script.Serialization;
using ShopOnline.Controls.BusinessCard;
namespace ShopOnline
{
    public partial class CreateCard : BasePage
    {
        View_Customers _user;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((GeneralMaster)this.Master).SetPageTitle("Create New Card");
            _user = View_Customers.GetCurrentUser();
            if (!IsPostBack)
            {
                Refresh();
            }
        }

        private void Refresh()
        {
            if (_user != null)
            {
                BCCard listCard = BCCard.GetMyCards(_user.ID);
                dtlListCard.DataSource = listCard.DefaultView;
                dtlListCard.DataBind();
            }

            BCCard listTemplateCard = BCCard.GetTemplateCards();
            dtlListTemplateCard.DataSource = listTemplateCard.DefaultView;
            dtlListTemplateCard.DataBind();
        }

        [WebMethod]
        public static void SaveAsDraft(string json)
        {
            Save(json, false, BCCard.StatusCard.Draft);
        }

        [WebMethod]
        public static void SaveAsTemplate(string json)
        {
            Save(json, true, BCCard.StatusCard.Draft);
        }

        [WebMethod]
        public static void SaveAndSubmit(string json)
        {
            Save(json, false, BCCard.StatusCard.Submitted);
        }

        private static void Save(string json, bool IsTemplate, BCCard.StatusCard status)
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();

                BCCard bcCard = new BCCard();
                bcCard.AddNew();

                BCCardDetail bcFrontCard = new BCCardDetail();
                bcFrontCard.AddNew();
                BCCardDetail bcBackCard = new BCCardDetail();
                bcBackCard.AddNew();
                BCTextBlock bcTextBlockFrontCard = new BCTextBlock();
                BCTextBlock bcTextBlockBackCard = new BCTextBlock();

                BCCardContact bcCardContact = new BCCardContact();

                JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                Dictionary<string, object> card =
                       (Dictionary<string, object>)json_serializer.DeserializeObject(json);

                if (card.Count > 0)
                {
                    // Loop over pairs with foreach
                    foreach (KeyValuePair<string, object> pair in card)
                    {
                        if (pair.Key.ToLower() == "bordertype")
                        {
                            bcCard.s_BorderType = (string)pair.Value;
                        }
                        else if (pair.Key.ToLower() == "frontcard")
                        {
                            Dictionary<string, object> frontCard = (Dictionary<string, object>)pair.Value;
                            if (frontCard.Count > 0)
                            {
                                foreach (KeyValuePair<string, object> cardDetail in frontCard)
                                {
                                    if (cardDetail.Key.ToLower() == "background")
                                    {
                                        bcFrontCard.s_Background = (string)cardDetail.Value;
                                        if (!string.IsNullOrEmpty(bcFrontCard.s_Background) && !bcFrontCard.s_Background.Equals("none"))
                                        {
                                            CopyImageToOriginalFolder(bcFrontCard.s_Background);
                                        }
                                    }
                                    else if (cardDetail.Key.ToLower() == "logo")
                                    {
                                        bcFrontCard.s_Logo = (string)cardDetail.Value;
                                        if (!string.IsNullOrEmpty(bcFrontCard.s_Logo))
                                        {
                                            CopyImageToOriginalFolder(bcFrontCard.s_Logo);
                                        }
                                    }
                                    else if (cardDetail.Key.ToLower() == "logopositionx")
                                    {
                                        bcFrontCard.s_LogoPositionX = (string)cardDetail.Value;
                                    }
                                    else if (cardDetail.Key.ToLower() == "logopositiony")
                                    {
                                        bcFrontCard.s_LogoPositionY = (string)cardDetail.Value;
                                    }
                                    else if (cardDetail.Key.ToLower() == "listtextblock")
                                    {
                                        bcFrontCard.Save();

                                        object[] listTextBlock = (object[])cardDetail.Value;
                                        if (listTextBlock.Length > 0)
                                        {
                                            foreach (object obj in listTextBlock)
                                            {
                                                Dictionary<string, object> textblock = (Dictionary<string, object>)obj;
                                                if (textblock.Count > 0)
                                                {
                                                    bcTextBlockFrontCard.AddNew();
                                                    foreach (KeyValuePair<string, object> textblockDetail in textblock)
                                                    {
                                                        if (textblockDetail.Key.ToLower() == "positionleft")
                                                        {
                                                            bcTextBlockFrontCard.PositionLeft = Convert.ToInt32(textblockDetail.Value);
                                                        }
                                                        else if (textblockDetail.Key.ToLower() == "positiontop")
                                                        {
                                                            bcTextBlockFrontCard.PositionTop = Convert.ToInt32(textblockDetail.Value);
                                                        }
                                                        else if (textblockDetail.Key.ToLower() == "text")
                                                        {
                                                            bcTextBlockFrontCard.s_Text = (string)textblockDetail.Value;
                                                        }
                                                    }
                                                    bcTextBlockFrontCard.s_CardID = bcFrontCard.s_ID;
                                                }
                                            }
                                            bcTextBlockFrontCard.Save();
                                        }
                                    }
                                }
                            }
                        }
                        else if (pair.Key.ToLower() == "backcard")
                        {
                            Dictionary<string, object> backCard = (Dictionary<string, object>)pair.Value;
                            if (backCard.Count > 0)
                            {
                                foreach (KeyValuePair<string, object> cardDetail in backCard)
                                {
                                    if (cardDetail.Key.ToLower() == "background")
                                    {
                                        bcBackCard.s_Background = (string)cardDetail.Value;
                                        if (!string.IsNullOrEmpty(bcBackCard.s_Background) && !bcBackCard.s_Background.Equals("none"))
                                        {
                                            CopyImageToOriginalFolder(bcBackCard.s_Background);
                                        }
                                    }
                                    else if (cardDetail.Key.ToLower() == "logo")
                                    {
                                        bcBackCard.s_Logo = (string)cardDetail.Value;
                                        if (!string.IsNullOrEmpty(bcBackCard.s_Logo))
                                        {
                                            CopyImageToOriginalFolder(bcBackCard.s_Logo);
                                        }
                                    }
                                    else if (cardDetail.Key.ToLower() == "logopositionx")
                                    {
                                        bcBackCard.s_LogoPositionX = (string)cardDetail.Value;
                                    }
                                    else if (cardDetail.Key.ToLower() == "logopositiony")
                                    {
                                        bcBackCard.s_LogoPositionY = (string)cardDetail.Value;
                                    }
                                    else if (cardDetail.Key.ToLower() == "listtextblock")
                                    {
                                        bcBackCard.Save();

                                        object[] listTextBlock = (object[])cardDetail.Value;
                                        if (listTextBlock.Length > 0)
                                        {
                                            foreach (object obj in listTextBlock)
                                            {
                                                Dictionary<string, object> textblock = (Dictionary<string, object>)obj;
                                                if (textblock.Count > 0)
                                                {
                                                    bcTextBlockBackCard.AddNew();
                                                    foreach (KeyValuePair<string, object> textblockDetail in textblock)
                                                    {
                                                        if (textblockDetail.Key.ToLower() == "positionleft")
                                                        {
                                                            bcTextBlockBackCard.PositionLeft = Convert.ToInt32(textblockDetail.Value);
                                                        }
                                                        else if (textblockDetail.Key.ToLower() == "positiontop")
                                                        {
                                                            bcTextBlockBackCard.PositionTop = Convert.ToInt32(textblockDetail.Value);
                                                        }
                                                        else if (textblockDetail.Key.ToLower() == "text")
                                                        {
                                                            bcTextBlockBackCard.s_Text = (string)textblockDetail.Value;
                                                        }
                                                    }
                                                    bcTextBlockBackCard.s_CardID = bcBackCard.s_ID;
                                                }
                                            }
                                            bcTextBlockBackCard.Save();
                                        }
                                    }
                                }
                            }
                        }
                        else if (pair.Key.ToLower() == "cardcontact")
                        {
                            Dictionary<string, object> cardContact = (Dictionary<string, object>)pair.Value;
                            if (cardContact.Count > 0)
                            {
                                bcCardContact.AddNew();
                                foreach (KeyValuePair<string, object> contact in cardContact)
                                {
                                    if (contact.Key.ToLower() == "name")
                                    {
                                        bcCardContact.Name = (string)contact.Value;
                                    }
                                    else if (contact.Key.ToLower() == "address")
                                    {
                                        bcCardContact.Address = (string)contact.Value;
                                    }
                                    else if (contact.Key.ToLower() == "phone")
                                    {
                                        bcCardContact.Phone = (string)contact.Value;
                                    }
                                    else if (contact.Key.ToLower() == "email")
                                    {
                                        bcCardContact.Email = (string)contact.Value;
                                    }
                                }
                            }
                        }
                    }
                    bcCard.s_FrontCardID = bcFrontCard.s_ID;
                    bcCard.s_BackCardID = bcBackCard.s_ID;
                    bcCard.IsTemplate = IsTemplate;
                    bcCard.CustomerID = 1;
                    bcCard.StatusID = (int)status;//Draft
                    bcCard.CreatedDate = System.DateTime.Now;
                    bcCard.CreatedByUser = 1;
                    bcCard.Save();

                    //Save Card Contact
                    bcCardContact.CardID = bcCard.ID;
                    bcCardContact.Save();
                }

                tx.CommitTransaction();
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
            }
        }

        private static void CopyImageToOriginalFolder(string sImageName)
        {
            try
            {
                var TempThumbFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utilities.GetKeySetting("TempThumbFolder"));
                var TempOriginalFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utilities.GetKeySetting("TempOriginalFolder"));

                var ThumbFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utilities.GetKeySetting("ThumbFolder"));
                var OriginalFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utilities.GetKeySetting("OriginalFolder"));

                DirectoryInfo dInfo = new DirectoryInfo(ThumbFolder);
                if (!dInfo.Exists) dInfo.Create();
                dInfo = new DirectoryInfo(OriginalFolder);
                if (!dInfo.Exists) dInfo.Create();

                FileInfo fInfo1 = new FileInfo(Path.Combine(TempThumbFolder, sImageName));
                if (fInfo1.Exists)
                {
                    fInfo1.CopyTo(Path.Combine(ThumbFolder, sImageName), true);
                }
                FileInfo fInfo2 = new FileInfo(Path.Combine(TempOriginalFolder, sImageName));
                if (fInfo2.Exists)
                {
                    fInfo2.CopyTo(Path.Combine(OriginalFolder, sImageName), true);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}